package ssi.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Statement;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

import ssi.tools.Connector;
import ssi.beans.Ads;
import ssi.beans.User;

public class AddAdAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute("user");
		Ads ads = (Ads) form;
		String title = ads.getTitle();
		String ad = ads.getAd();
		FormFile file = ads.getFile();
		int animalId = Integer.parseInt(request.getParameter("animal_choice"));
		int userId = user.getId();
		Calendar calendar = Calendar.getInstance();
		Date data = new Date(calendar.getTime().getTime());
		String date = data.toString();
		ActionErrors actionErrors = new ActionErrors();
		String target = null;
		
		String filePath = "D:/Dropbox/Studia/Semestr VII/Sieciowe systemy informatyczne/Moje/Projekt/SSIProject/SSIProject/WebContent/images/upload";
		File folder = new File(filePath);
		if (!folder.exists()) {
			folder.mkdir();
		}

		String fileName = file.getFileName();
		try {
			if (!(title.equals("") || ad.equals("") || animalId == 0
					|| userId == 0 || date.equals(""))) {
				if (!("").equals(fileName)) {

					
					File newFile = new File(filePath, fileName);

					if (!newFile.exists()) {
						FileOutputStream fos = new FileOutputStream(newFile);
						fos.write(file.getFileData());
						fos.flush();
						fos.close();
					}

					System.out.println(title + " " + ad + " " + animalId + " "
							+ userId + " " + date + " " + newFile);
					Connection connection = new Connector().getConnection();
					Statement statement = connection.createStatement();
					statement
							.executeUpdate("insert into ad(date , title ,ad, userId, animalId,images) values(\""
									+ date
									+ "\",\""
									+ title
									+ "\",\""
									+ ad
									+ "\",\""
									+ userId
									+ "\",\""
									+ animalId
									+ "\",\"" 
									+ fileName + "\");");
					target = "success";
					actionErrors.add("error", new ActionMessage(
							"error.addAdSuccessfull"));
					connection.close();
				}
			} else {
				target = "failure";
				actionErrors.add("error",
						new ActionMessage("error.emptyFields"));
			}

		} catch (Exception e) {
			target = "failure";
			actionErrors.add("error", new ActionMessage("error.catch"));
		}

		if (actionErrors.size() != 0) {
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward(target);
	}

	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}
}
